﻿
namespace FlexPlatform.Visual.Presenters
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;

    using FlexPlatform.Visual.Presenters;

    using FlexPlatform.Shared.DataModel;
    using FlexPlatform.Shared.Presenters;

    public class SectionPresenter : ISectionPresenter
    {
        private Collection<IEntityWithPresenter> clsDataEntities;

        public SectionPresenter()
        {
            clsDataEntities = new Collection<IEntityWithPresenter>();
        }

        public string Name { get; set; }
        
        public bool IsCollapsed { get; set; }

        public bool IsBusy
        {
            get
            {
                return (clsDataEntities.Any(x => x.Presenter.IsBusy));
            }
        }

        [ThreadStatic]
        private bool clsEntityRegistrationStarted;
        public void RegisterDataEntity(IEntityWithPresenter dataEntity)
        {
            // don't do anything if another entity registration have been started from some other entry point
            if (!clsEntityRegistrationStarted)
            {
                clsEntityRegistrationStarted = true;
                if (!clsDataEntities.Contains(dataEntity))
                {
                    clsDataEntities.Add(dataEntity);

                    // link back the DataEntityPresenter.Section property.
                    // dataEntity.Presenter.Section = this;

                }
                clsEntityRegistrationStarted = false;
            }
            
        }
    }
}
